package feeinterface.protocol.business;

import org.apache.log4j.Logger;

import com.ulwx.tool.StringUtils;
import com.ulwx.type.TString;

import feeinterface.protocol.res.CmdAddr;
import feeinterface.protocol.res.FetchStep;

/**
 * 解析抓取方法
 * 
 * @author 黄杰
 * 
 */
public class ParseFetch {
	/**
	 * 
	 */
	private static final Logger log = Logger.getLogger(ParseFetch.class);

	/**
	 * 解析抓取步骤
	 * 
	 * @param fetchStep
	 * @return
	 */
	public static FetchStep[] analyse(String fetchStep) {
		FetchStep feeStep[] = new FetchStep[0];
		try {
			if (StringUtils.hasText(fetchStep)) {
				String wapStep[] = StringUtils.split(fetchStep, "=@=");
				if (wapStep != null && wapStep.length > 0) {
					feeStep = new FetchStep[wapStep.length];
					for (int i = 0; i < wapStep.length; i++) {
						String key[] = StringUtils.split(wapStep[i], "@@");
						feeStep[i] = new FetchStep();
						feeStep[i].FetchBy = Integer.parseInt(key[0]);
						feeStep[i].FindKeys = key[1];
						feeStep[i].EndKey = key[2];
					}
				}
			} else {
				feeStep = new FetchStep[0];
				;
			}
		} catch (Exception e) {
			log.error("", e);
			feeStep = new FetchStep[0];
			;
		}
		return feeStep;
	}

	/**
	 * 解析抓取步骤 解析格式Cmd1@@DestAddr1=@=Cmd2@@DestAddr2=@=...
	 * 
	 * @param cmd
	 * @param addr
	 * @param sendCnt
	 * @param parse
	 * @return
	 */
	public static CmdAddr[] parse(String cmd, String addr, int sendCnt,
			String parse) {
		CmdAddr ca[] = new CmdAddr[1];
		ca[0] = new CmdAddr();
		ca[0].Cmd = cmd;
		ca[0].Addr = addr;
		ca[0].extSmsVar="StCnt=1";
		try {
			if (parse != null && !"".equals(parse)) {
				String step[] = StringUtils.split(parse, "=@=");
				if (step != null && step.length > 0) {
					int length = step.length + 1;// 一组双短信通道
					ca = new CmdAddr[length * sendCnt];// 增加循环次数通道
					for (int i = 0; i < sendCnt; i++) {
						// 2表示双短信通道，发送次数
						int j = i * length;
						// 主指令
						ca[j] = new CmdAddr();
						ca[j].Cmd = cmd;
						ca[j].Addr = addr;
						ca[j].extSmsVar="StCnt=1";
						// 辅指令
						for (int k = 0; k < step.length; k++) {
							String key[] = StringUtils.split(step[k], "@@");
							ca[j + k + 1] = new CmdAddr();
							ca[j + k + 1].Cmd = key[0];
							ca[j + k + 1].Addr = key[1];
							ca[j + k + 1].extSmsVar="StCnt=0";
						}
					}
				}
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return ca;
	}

	/**
	 * 截取字符串
	 * 
	 * @param oldStr
	 * @param start
	 * @param end
	 * @return
	 */
	public static String StringIntercept(String oldStr, String sKeys,
			String endKey) {
		String newStr = oldStr;
		int startIndex = 0, endIndex = 0;
		try {
			if (sKeys == null || StringUtils.isEmpty(sKeys)) {
				endIndex = oldStr.indexOf(endKey);
				return oldStr.substring(0, endIndex);
			}
			String keys[] = StringUtils.split(sKeys, ",");
			if (keys != null && keys.length > 0) {
				for (String key : keys) {
					startIndex = oldStr.indexOf(key);
					if (startIndex >= 0) {
						startIndex = startIndex + key.length();
					} else {
						startIndex = 0;
						continue;
					}
					oldStr = oldStr.substring(startIndex);
				}
			}
			if (null != endKey && !"".equals(endKey) && StringUtils.hasText(endKey)) {
				endIndex = oldStr.indexOf(endKey);
			} else {
				endIndex = -1;
			}
			if (endIndex >= 0) {
				newStr = oldStr.substring(0, endIndex);
			} else {
				newStr = oldStr;
			}
		} catch (Exception e) {
			log.error("", e);
		}
		return newStr;
	}

	public static void main(String arg[]) throws Exception {
		String a = "您将使用广州卓讯公司的开心时刻业务，信息费2.00元/次，客服电话020-38105196，请回复问题确认点播：首的反义词是什么？";
		System.out.println(StringIntercept(a, "确认点播：", "？"));

	}
}
